Vue Scoped Slots 您所在的位置:网站首页 bootstrap thank you page template Vue Scoped Slots

Vue Scoped Slots

2023-04-25 19:28| 来源: 网络整理| 查看: 265

Scoped Slots ❮ Previous Next ❯

A Scoped slot provides local data from the component so that the parent can choose how to render it.

Send Data to Parent

We use v-bind in the component slot to send local data to the parent:

SlotComp.vue:

    export default {     data() {       return {         data: 'This is local data'       }     }   }

The data inside the component can be referred to as 'local' because it is not accessible to the parent unless it is sent up to the parent like we do here with v-bind.

Receive Data from Scoped Slot

The local data in the component is sent with v-bind, and it can be received in the parent with v-slot:

Example

App.vue:

  {{ dataFromSlot.lclData }} Run Example 禄

In the example above, 'dataFromSlot' is just a name we can choose ourselves to represent the data object we receive from the scoped slot. We get the text string from the slot by using the 'lclData' property, and we use interpolation to finally render the text in an tag.

Scoped Slot with an Array

A scoped slot can send data from an array by using v-for, but the code in App.vue is basically the same:

Example

SlotComp.vue:

    export default {     data() {       return {         foods: ['Apple','Pizza','Rice','Fish','Cake']       }     }   }

App.vue:

  {{ food.foodName }} Run Example 禄 Scoped Slot with an Array of Objects

A scoped slot can send data from an array of objects by using v-for:

Example

SlotComp.vue:

    export default {     data() {       return {         foods: [           { name: 'Apple', desc: 'Apples are a type of fruit that grow on trees.', url: 'img_apple.svg' },           { name: 'Pizza', desc: 'Pizza has a bread base with tomato sauce, cheese, and toppings on top.', url: 'img_pizza.svg' },           { name: 'Rice', desc: 'Rice is a type of grain that people like to eat.', url: 'img_rice.svg' },           { name: 'Fish', desc: 'Fish is an animal that lives in water.', url: 'img_fish.svg' },           { name: 'Cake', desc: 'Cake is something sweet that tastes good but is not considered healthy.', url: 'img_cake.svg' }        ]       }     }   }

App.vue:

    {{ food.foodName }}  

{{ food.foodDesc }}

Run Example 禄 Static Data from a Scoped Slot

A scoped slot can also send static data, that is data that does not belong to the data property of the Vue instance.

When sending static data we do not use v-bind.

In the example below we send one static text, and one text bound dynamically to the data instance so that we can see the difference.

Example

SlotComp.vue:

    export default {     data() {       return {         text: 'This text is from the data property'       }     }   }

App.vue:

  {{ texts.staticText }}  

{{ texts.dynamicText }}

Run Example 禄 Named Scoped Slots

Scoped slots can be named.

To use named scoped slots we need to name the slots inside the component with the 'name' attribute.

And to receive data from a named slot we need to refer to that name in the parent where we use the component, with the v-slot directive, or shorthand #.

Example

In this example the component is created one time referring to the slot "leftSlot", and one time referring to the slot "rightSlot".

SlotComp.vue:

      export default {     data() {       return {         leftText: 'This text belongs to the LEFT slot.',         rightText: 'This text belongs to the RIGHT slot.'       }     }   }

App.vue:

  {{ leftProps.text }}   {{ rightProps.text }} Run Example 禄

Alternatively, we can create the component one time, with two different "template" tags, each "template" tag referring to a different slot.

Example

In this example the component is created only one time, but with two "template" tags, each referring to a different slot.

SlotComp.vue is exactly the same as in the previous example.

App.vue:

      {{ leftProps.text }}         {{ rightProps.text }}   Run Example 禄 Vue Exercises ❮ Previous Next ❯


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有